#Copyright (c) 2003 - 2012 Roberto Rodriguez

#This program is free software; you can redistribute it and/or modify
#it under the terms of version 2 of the GNU General Public License
#as published by the Free Software Foundation.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License along
#with this program; if not, write to the Free Software Foundation, Inc.,
#51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

SetTitle "Compile crt0.o"
gcc ../../rymos/libc/crt0.S -o $CROSSCOMP/$MYOS/lib/crt0.o -c -O2 -Wall || exit

SetTitle "Compile libc"
mkdir -p libc-build/arch
mkdir -p libc-build/src
array_files_i386=(
        ../../rymos/libc/arch/i386/syscall.S
        ../../rymos/libc/arch/i386/getpagesize.c
        ../../rymos/libc/arch/i386/setjmp.S
        ../../rymos/libc/arch/i386/longjmp.S
        ../../rymos/libc/arch/i386/memcpy.S
        ../../rymos/libc/arch/i386/atomic.S
        ../../rymos/libc/arch/i386/math/e_exp.S
        ../../rymos/libc/arch/i386/math/e_log.S
        ../../rymos/libc/arch/i386/math/s_sin.S
        ../../rymos/libc/arch/i386/math/s_cos.S
        ../../rymos/libc/arch/i386/math/s_floor.S
        ../../rymos/libc/arch/i386/math/s_ceil.S
        ../../rymos/libc/arch/i386/math/s_frexp.S
        ../../rymos/libc/arch/i386/math/s_fabs.S
        ../../rymos/libc/arch/i386/math/e_fmod.S
        ../../rymos/libc/arch/i386/math/e_atan2.S
        ../../rymos/libc/arch/i386/math/e_hypot.S
        ../../rymos/libc/arch/i386/math/e_pow.S
        ../../rymos/libc/arch/i386/math/s_finite.S
        ../../rymos/libc/arch/i386/math/s_scalbn.S
        ../../rymos/libc/arch/i386/math/e_log10.S
        ../../rymos/libc/arch/i386/math/s_tan.S
        ../../rymos/libc/arch/i386/math/s_atan.S
        ../../rymos/libc/arch/i386/math/e_sqrt.S
        ../../rymos/libc/arch/i386/math/e_acos.S
        ../../rymos/libc/arch/i386/math/e_asin.S
        ../../rymos/libc/arch/i386/math/e_expl.c
    )

array_files=(
        ../../rymos/libc/src/start.c
        ../../rymos/libc/src/opendir.c
        ../../rymos/libc/src/readdir.c
        ../../rymos/libc/src/closedir.c
        ../../rymos/libc/src/rewinddir.c
        ../../rymos/libc/src/stdlib/malloc.c
        ../../rymos/libc/src/stdlib/abort.c
        ../../rymos/libc/src/stdlib/getenv.c
        ../../rymos/libc/src/stdlib/strtol.c
        ../../rymos/libc/src/stdlib/strtoll.c
        ../../rymos/libc/src/stdlib/strtoul.c
        ../../rymos/libc/src/stdlib/strtoull.c
        ../../rymos/libc/src/stdlib/atoi.c
        ../../rymos/libc/src/stdlib/atol.c
        ../../rymos/libc/src/stdlib/atoll.c
        ../../rymos/libc/src/stdlib/atof.c
        ../../rymos/libc/src/stdlib/qsort.c
        ../../rymos/libc/src/stdlib/bsearch.c
        ../../rymos/libc/src/stdlib/abs.c
        ../../rymos/libc/src/stdlib/labs.c
        ../../rymos/libc/src/stdlib/llabs.c
        ../../rymos/libc/src/stdlib/strtod.c
        ../../rymos/libc/src/stdlib/mkstemp.c
        ../../rymos/libc/src/stdlib/random.c
        ../../rymos/libc/src/stdlib/srandom.c
        ../../rymos/libc/src/stdlib/rand.c
        ../../rymos/libc/src/stdlib/srand.c
        ../../rymos/libc/src/stdlib/mktemp.c
        ../../rymos/libc/src/stdlib/system.c
        ../../rymos/libc/src/string/memset.c
        ../../rymos/libc/src/string/memcpy.c
        ../../rymos/libc/src/string/memcmp.c
        ../../rymos/libc/src/string/memmove.c
        ../../rymos/libc/src/string/memchr.c
        ../../rymos/libc/src/string/strchr.c
        ../../rymos/libc/src/string/strrchr.c
        ../../rymos/libc/src/string/strlen.c
        ../../rymos/libc/src/string/strnlen.c
        ../../rymos/libc/src/string/strcmp.c
        ../../rymos/libc/src/string/strncmp.c
        ../../rymos/libc/src/string/strcasecmp.c
        ../../rymos/libc/src/string/strncasecmp.c
        ../../rymos/libc/src/string/strcoll.c
        ../../rymos/libc/src/string/strdup.c
        ../../rymos/libc/src/string/strndup.c
        ../../rymos/libc/src/string/strcpy.c
        ../../rymos/libc/src/string/strncpy.c
        ../../rymos/libc/src/string/strstr.c
        ../../rymos/libc/src/string/strerror.c
        ../../rymos/libc/src/string/strsignal.c
        ../../rymos/libc/src/string/strcat.c
        ../../rymos/libc/src/string/strncat.c
        ../../rymos/libc/src/string/strpbrk.c
        ../../rymos/libc/src/string/strspn.c
        ../../rymos/libc/src/string/strcspn.c
        ../../rymos/libc/src/string/strtok_r.c
        ../../rymos/libc/src/string/strtok.c
        ../../rymos/libc/src/string/strxfrm.c
        ../../rymos/libc/src/string/strtof.c
        ../../rymos/libc/src/string/ffs.c
        ../../rymos/libc/src/string/strerror_r.c
        ../../rymos/libc/src/unistd/sync.c
        ../../rymos/libc/src/unistd/sbrk.c
        ../../rymos/libc/src/unistd/fork.c
        ../../rymos/libc/src/unistd/execve.c
        ../../rymos/libc/src/unistd/execv.c
        ../../rymos/libc/src/unistd/execl.c
        ../../rymos/libc/src/unistd/execlp.c
        ../../rymos/libc/src/unistd/dup.c
        ../../rymos/libc/src/unistd/dup2.c
        ../../rymos/libc/src/unistd/write.c
        ../../rymos/libc/src/unistd/read.c
        ../../rymos/libc/src/unistd/pwrite.c
        ../../rymos/libc/src/unistd/pread.c
        ../../rymos/libc/src/unistd/exit.c
        ../../rymos/libc/src/unistd/getdents.c
        ../../rymos/libc/src/unistd/close.c
        ../../rymos/libc/src/unistd/execvp.c
        ../../rymos/libc/src/unistd/fchdir.c
        ../../rymos/libc/src/unistd/isatty.c
        ../../rymos/libc/src/unistd/lseek.c
        ../../rymos/libc/src/unistd/unlink.c
        ../../rymos/libc/src/unistd/readlink.c
        ../../rymos/libc/src/unistd/getcwd.c
        ../../rymos/libc/src/unistd/sleep.c
        ../../rymos/libc/src/unistd/access.c
        ../../rymos/libc/src/unistd/chdir.c
        ../../rymos/libc/src/unistd/ftruncate.c
        ../../rymos/libc/src/unistd/getpid.c
        ../../rymos/libc/src/unistd/getppid.c
        ../../rymos/libc/src/unistd/gettid.c
        ../../rymos/libc/src/unistd/link.c
        ../../rymos/libc/src/unistd/rmdir.c
        ../../rymos/libc/src/unistd/chown.c
        ../../rymos/libc/src/unistd/symlink.c
        ../../rymos/libc/src/unistd/ttyname.c
        ../../rymos/libc/src/unistd/pipe.c
        ../../rymos/libc/src/unistd/alarm.c
        ../../rymos/libc/src/unistd/fpathconf.c
        ../../rymos/libc/src/unistd/getuid.c
        ../../rymos/libc/src/unistd/geteuid.c
        ../../rymos/libc/src/unistd/setuid.c
        ../../rymos/libc/src/unistd/setreuid.c
        ../../rymos/libc/src/unistd/getgid.c
        ../../rymos/libc/src/unistd/getegid.c
        ../../rymos/libc/src/unistd/setgid.c
        ../../rymos/libc/src/unistd/setregid.c
        ../../rymos/libc/src/unistd/gethostname.c
        ../../rymos/libc/src/unistd/getdtablesize.c
        ../../rymos/libc/src/unistd/getpgid.c
        ../../rymos/libc/src/unistd/setpgid.c
        ../../rymos/libc/src/unistd/getpgrp.c
        ../../rymos/libc/src/unistd/setpgrp.c
        ../../rymos/libc/src/unistd/usleep.c
        ../../rymos/libc/src/fcntl/open.c
        ../../rymos/libc/src/fcntl/creat.c
        ../../rymos/libc/src/fcntl/fcntl.c
        ../../rymos/libc/src/stdio/stdio_internal.c
        ../../rymos/libc/src/stdio/streams.c
        ../../rymos/libc/src/stdio/ferror.c
        ../../rymos/libc/src/stdio/fgetc.c
        ../../rymos/libc/src/stdio/getc.c
        ../../rymos/libc/src/stdio/fgets.c
        ../../rymos/libc/src/stdio/fputc.c
        ../../rymos/libc/src/stdio/fputs.c
        ../../rymos/libc/src/stdio/fileno.c
        ../../rymos/libc/src/stdio/feof.c
        ../../rymos/libc/src/stdio/fflush.c
        ../../rymos/libc/src/stdio/ungetc.c
        ../../rymos/libc/src/stdio/clearerr.c
        ../../rymos/libc/src/stdio/fopen.c
        ../../rymos/libc/src/stdio/fdopen.c
        ../../rymos/libc/src/stdio/freopen.c
        ../../rymos/libc/src/stdio/fclose.c
        ../../rymos/libc/src/stdio/popen.c
        ../../rymos/libc/src/stdio/pclose.c
        ../../rymos/libc/src/stdio/putc.c
        ../../rymos/libc/src/stdio/setvbuf.c
        ../../rymos/libc/src/stdio/fseek.c
        ../../rymos/libc/src/stdio/ftell.c
        ../../rymos/libc/src/stdio/fread.c
        ../../rymos/libc/src/stdio/fwrite.c
        ../../rymos/libc/src/stdio/rewind.c
        ../../rymos/libc/src/stdio/perror.c
        ../../rymos/libc/src/stdio/puts.c
        ../../rymos/libc/src/stdio/putchar.c
        ../../rymos/libc/src/stdio/rename.c
        ../../rymos/libc/src/stdio/remove.c
        ../../rymos/libc/src/stdio/fpurge.c
        ../../rymos/libc/src/stdio/fgetpos.c
        ../../rymos/libc/src/stdio/fsetpos.c
        ../../rymos/libc/src/stdio/getchar.c
        ../../rymos/libc/src/stdio/gets.c
        ../../rymos/libc/src/stdio/setbuf.c
        ../../rymos/libc/src/stdio/tmpfile.c
        ../../rymos/libc/src/stdio/tmpnam.c
        ../../rymos/libc/src/time/tzset.c
        ../../rymos/libc/src/time/time_int.c
        ../../rymos/libc/src/time/strftime.c
        ../../rymos/libc/src/time/mktime.c
        ../../rymos/libc/src/time/ctime.c
        ../../rymos/libc/src/time/ctime_r.c
        ../../rymos/libc/src/time/localtime.c
        ../../rymos/libc/src/time/localtime_r.c
        ../../rymos/libc/src/time/asctime.c
        ../../rymos/libc/src/time/asctime_r.c
        ../../rymos/libc/src/time/gmtime.c
        ../../rymos/libc/src/time/gmtime_r.c
        ../../rymos/libc/src/time/gettimeofday.c
        ../../rymos/libc/src/time/ctime.c
        ../../rymos/libc/src/time/time.c
        ../../rymos/libc/src/time/nanosleep.c
        ../../rymos/libc/src/time/clock.c
        ../../rymos/libc/src/getopt/getopt.c
        ../../rymos/libc/src/getopt/getopt_long.c
        ../../rymos/libc/src/getopt/getopt_long_only.c
        ../../rymos/libc/src/sys/stat.c
        ../../rymos/libc/src/sys/fstat.c
        ../../rymos/libc/src/sys/stime.c
        ../../rymos/libc/src/sys/mkdir.c
        ../../rymos/libc/src/sys/mknod.c
        ../../rymos/libc/src/sys/ioctl.c
        ../../rymos/libc/src/sys/select.c
        ../../rymos/libc/src/sys/mount.c
        ../../rymos/libc/src/sys/umount.c
        ../../rymos/libc/src/sys/lstat.c
        ../../rymos/libc/src/sys/umask.c
        ../../rymos/libc/src/sys/chmod.c
        ../../rymos/libc/src/sys/utime.c
        ../../rymos/libc/src/sys/utimes.c
        ../../rymos/libc/src/sys/wait.c
        ../../rymos/libc/src/sys/wait3.c
        ../../rymos/libc/src/sys/wait4.c
        ../../rymos/libc/src/sys/waitpid.c
        ../../rymos/libc/src/sys/socket.c
        ../../rymos/libc/src/sys/connect.c
        ../../rymos/libc/src/sys/bind.c
        ../../rymos/libc/src/sys/listen.c
        ../../rymos/libc/src/sys/accept.c
        ../../rymos/libc/src/sys/recv.c
        ../../rymos/libc/src/sys/recvfrom.c
        ../../rymos/libc/src/sys/recvmsg.c
        ../../rymos/libc/src/sys/send.c
        ../../rymos/libc/src/sys/sendto.c
        ../../rymos/libc/src/sys/sendmsg.c
        ../../rymos/libc/src/sys/shutdown.c
        ../../rymos/libc/src/sys/getsockopt.c
        ../../rymos/libc/src/sys/setsockopt.c
        ../../rymos/libc/src/sys/getsockname.c
        ../../rymos/libc/src/sys/getpeername.c
        ../../rymos/libc/src/sys/getrusage.c
        ../../rymos/libc/src/ctype/isalpha.c
        ../../rymos/libc/src/ctype/isupper.c
        ../../rymos/libc/src/ctype/isblank.c
        ../../rymos/libc/src/ctype/islower.c
        ../../rymos/libc/src/ctype/isdigit.c
        ../../rymos/libc/src/ctype/isxdigit.c
        ../../rymos/libc/src/ctype/isalnum.c
        ../../rymos/libc/src/ctype/isspace.c
        ../../rymos/libc/src/ctype/isprint.c
        ../../rymos/libc/src/ctype/iscntrl.c
        ../../rymos/libc/src/ctype/tolower.c
        ../../rymos/libc/src/ctype/toupper.c
        ../../rymos/libc/src/ctype/toascii.c
        ../../rymos/libc/src/ctype/isgraph.c
        ../../rymos/libc/src/ctype/ispunct.c
        ../../rymos/libc/src/ctype/isascii.c
        ../../rymos/libc/src/ctype/ctype.c
        ../../rymos/libc/src/signal/signal.c
        ../../rymos/libc/src/signal/sigaction.c
        ../../rymos/libc/src/signal/kill.c
        ../../rymos/libc/src/signal/killpg.c
        ../../rymos/libc/src/signal/raise.c
        ../../rymos/libc/src/signal/sigemptyset.c
        ../../rymos/libc/src/signal/sigfillset.c
        ../../rymos/libc/src/signal/sigaddset.c
        ../../rymos/libc/src/signal/sigdelset.c
        ../../rymos/libc/src/signal/sigismember.c
        ../../rymos/libc/src/signal/sigprocmask.c
        ../../rymos/libc/src/signal/sigsetjmp.c
        ../../rymos/libc/src/signal/siglongjmp.c
        ../../rymos/libc/src/locale/localeconv.c
        ../../rymos/libc/src/locale/setlocale.c
        ../../rymos/libc/src/termios/tcflush.c
        ../../rymos/libc/src/termios/tcgetattr.c
        ../../rymos/libc/src/termios/tcsetattr.c
        ../../rymos/libc/src/termios/tcflow.c
        ../../rymos/libc/src/termios/tcgetpgrp.c
        ../../rymos/libc/src/termios/tcsetpgrp.c
        ../../rymos/libc/src/math/s_ldexp.c
        ../../rymos/libc/src/math/s_modf.c
        ../../rymos/libc/src/math/s_lround.c
        ../../rymos/libc/src/math/math_dummy.c
        ../../rymos/libc/src/math/fabsf.c
        ../../rymos/libc/src/math/sqrtf.c
        ../../rymos/libc/src/rymos/debug.c
        ../../rymos/libc/src/rymos/sysinfo.c
        ../../rymos/libc/src/rymos/region.c
        ../../rymos/libc/src/rymos/module.c
        ../../rymos/libc/src/rymos/yaosp.c
        ../../rymos/libc/src/rymos/ipc.c
        ../../rymos/libc/src/rymos/config.c
        ../../rymos/libc/src/trio/trio.c
        ../../rymos/libc/src/trio/trionan.c
        ../../rymos/libc/src/trio/triostr.c
        ../../rymos/libc/src/pwd/getpwuid.c
        ../../rymos/libc/src/pwd/getpwent.c
        ../../rymos/libc/src/pwd/getpwnam.c
        ../../rymos/libc/src/pwd/endpwent.c
        ../../rymos/libc/src/pwd/setpwent.c
        ../../rymos/libc/src/network/inet_aton.c
        ../../rymos/libc/src/network/inet_pton.c
        ../../rymos/libc/src/network/inet_ntoa.c
        ../../rymos/libc/src/network/inet_ntop.c
        ../../rymos/libc/src/network/inet_addr.c
        ../../rymos/libc/src/network/gethostbyname.c
        ../../rymos/libc/src/network/gethostbyaddr.c
        ../../rymos/libc/src/network/getservbyport.c
        ../../rymos/libc/src/network/getservbyname.c
        ../../rymos/libc/src/network/getprotobyname.c
        ../../rymos/libc/src/network/dns.c
        ../../rymos/libc/src/pthread/mutex.c
        ../../rymos/libc/src/pthread/mutexattr.c
        ../../rymos/libc/src/pthread/thread.c
        ../../rymos/libc/src/pthread/attr.c
        ../../rymos/libc/src/pthread/condition.c
        ../../rymos/libc/src/pthread/once.c
        ../../rymos/libc/src/pthread/key.c
        ../../rymos/libc/src/regex/collate.c
        ../../rymos/libc/src/regex/collcmp.c
        ../../rymos/libc/src/regex/regcomp.c
        ../../rymos/libc/src/regex/regerror.c
        ../../rymos/libc/src/regex/regexec.c
        ../../rymos/libc/src/regex/regfree.c
        ../../rymos/libc/src/dl/dlopen.c
        ../../rymos/libc/src/dl/dlsym.c
        ../../rymos/libc/src/dl/dlclose.c
        ../../rymos/libc/src/dl/dlerror.c
        ../../rymos/libc/src/pty/openpty.c
    )
for ((i=0; i<=26; i++)); do
#echo "Index $i has: ${array_files_i386[i]}"
filename=$(basename "${array_files_i386[i]}")
extension="${filename##*.}"
filename2="${filename%.*}"
#echo "$filename"
#echo "$extension"
#echo "$filename2"
#echo "libc-build/arch/$filename2.o"
$MYOS-gcc ${array_files_i386[i]} -o libc-build/arch/$filename2.o -c -O2 -Wall -fno-builtin -g || exit
done

for ((i=0; i<=296; i++)); do
#echo "Index $i has: ${array_files[i]}"
filename=$(basename "${array_files[i]}")
extension="${filename##*.}"
filename2="${filename%.*}"
#echo "$filename"
#echo "$extension"
#echo "$filename2"
#echo "libc-build/src/$filename2.o"
$MYOS-gcc ${array_files[i]} -o libc-build/src/$filename2.o -c -O2 -Wall -fno-builtin -g -DHAVE_MMAP=0 -DUSE_LOCKS=1 -DRYMOS -DTRIO_FEATURE_LOCALE=0 -DTRIO_PLATFORM_UNIX || exit
done

$MYOS-ld -shared libc-build/src/*.o libc-build/arch/*.o -o $CROSSCOMP/$MYOS/lib/libc.so  || exit

$MYOS-ar -rc -o $CROSSCOMP/$MYOS/lib/libc.a libc-build/src/*.o  libc-build/arch/*.o  || exit

#cp $CROSSCOMP/lib/gcc/$MYOS/4.6.1/libc.so $CROSSCOMP/$MYOS/lib/libc.so
#cp $CROSSCOMP/lib/gcc/$MYOS/4.6.1/libc.a $CROSSCOMP/$MYOS/lib/libc.a
#cp $CROSSCOMP/lib/gcc/$MYOS/4.6.1/crt0.o $CROSSCOMP/$MYOS/lib/crt0.o
cp ../../rymos/libc/libg.a $CROSSCOMP/$MYOS/lib/libg.a
cp ../../rymos/libc/libm.a $CROSSCOMP/$MYOS/lib/libm.a
#cp ../../rymos/libc/libg.a $CROSSCOMP/lib/gcc/$MYOS/4.6.1/libg.a
#cp ../../rymos/libc/libm.a $CROSSCOMP/lib/gcc/$MYOS/4.6.1/libm.a

#SetTitle "Configure newlib 1.15.0 step 2"
#../newlib-1.15.0/configure --prefix=$CROSSCOMP --target=$MYOS || exit
#SetTitle "Compiling newlib 1.15.0"

